<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分割数组示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f3f4f6;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #ffffff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 90%;
            max-width: 600px;
        }

        h1 {
            text-align: center;
            color: #333;
            font-size: 24px;
            margin-bottom: 20px;
        }

        label {
            font-size: 16px;
            color: #555;
            margin-right: 10px;
        }

        .input {
            padding: 8px;
            font-size: 16px;
            margin: 10px 0;
            border-radius: 4px;
            border: 1px solid #ccc;
            width: 100%;
            box-sizing: border-box;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            margin-top: 10px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
        }

        .chunk {
            background-color: #f9f9f9;
            border-radius: 6px;
            margin: 10px 0;
            padding: 10px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .chunk span {
            font-weight: bold;
        }

        footer {
            text-align: center;
            margin-top: 30px;
            font-size: 14px;
            color: #888;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>分割数组示例</h1>
        <label for="inputArray">请输入一个数组（用逗号分隔）：</label>
        <textarea type="text" class="input" id="inputArray" placeholder="例如：1,2,3,4,5,6,7,8,9"></textarea>

        <label for="chunks">分割成多少组：</label>
        <input type="number" id="chunks" placeholder="请输入一个数字" min="1" class="input">

        <button id="splitButton">分割数组</button>

        <div id="result" class="result"></div>
    </div>



    <script>
        const chunkIntoN = (arr, n) => {
            const size = Math.ceil(arr.length / n);
            return Array.from({ length: n }, (v, i) =>
                arr.slice(size * i, size * i + size)
            );
        };

        document.getElementById('splitButton').addEventListener('click', () => {
            const inputArray = document.getElementById('inputArray').value;
            const chunks = document.getElementById('chunks').value;

            if (!inputArray || !chunks || chunks < 1) {
                alert('请输入有效的数组和分割数！');
                return;
            }

            const arr = inputArray.split(',').map(item => item.trim());
            const n = parseInt(chunks, 10);

            if (arr.length === 0 || n <= 0) {
                alert('数组或分割数无效！');
                return;
            }

            const chunkedArray = chunkIntoN(arr, n);
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = '';

            chunkedArray.forEach((chunk, index) => {
                const chunkDiv = document.createElement('div');
                chunkDiv.classList.add('chunk');
                chunkDiv.innerHTML = `<span>第 ${index + 1} 组：</span> ${chunk.join(', ')}`;
                resultDiv.appendChild(chunkDiv);
            });
        });
    </script>
</body>

</html>